<?php

namespace app\index\controller;

use app\index\model\IndexModel;
use think\Db;
use think\Session;

class Index extends Border
{

    public function index()
    {

        $id = Session::get('id');
//        halt($id);

        

        if ($id){


            $p = Db::field('photo')
                ->table(['wb_buserinfo'])
                ->where('uid = ' . $id)
                ->find();
            $photo = implode("", $p);
            $username = Session::get('username');
//            halt($id);
            return view('/index/index', [
                'list' => $this->list,
                'res' => $this->res,
                'banner' => $this->banner,
                'style'=>$this->style,
                'sort'=>$this->sort,
                'id' => $id,
                'data1'=>$this->data1,
                'username'=>$username,
                'photo'=>$photo

            ]);
        }else{
            return view('/index/index', [
                'list' => $this->list,
                'res' => $this->res,
                'banner' => $this->banner,
                'style'=>$this->style,
                'sort'=>$this->sort,
                'id' => $id,
                'data1'=>$this->data1


            ]);
        }

    }


    public function about($id)
    {


        $list = Db::field('m.id id,u.username username,u.id uid,photo,content,m.time,collectnum,commentnum,transpondnum,agreenum,hot,src_id')
            ->table(['wb_message' => 'm', 'wb_buser' => 'u', 'wb_style_message' => 'sm', 'wb_style' => 's', 'wb_buserinfo' => 'i'])
            ->where('m.uid = u.id and s.id = ' . $id . ' and sm.sid = s.id and m.id = sm.mid and i.uid=u.id ')
            ->order(['id' => 'desc'])
            ->select();
         // halt($list);
        $uid = Session::get('id');
        if (!empty($uid)) {
        	$p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = ' . $uid)
            ->find();
            $username = Session::get('username');

	         if (!empty($p)) {
	        	$photo = implode("", $p);
	         }
			foreach ($list as $k => $v) {
			            $path = Db::field('id pid,path')
			                ->table(['wb_mpicture'])
			                ->where('mid=' . $v['id'])
			                ->order(['id' => 'asc'])
			                ->select();
			            $list[$k]['path'] = $path;
			            if (!empty($v['src_id'])){
			//                halt($v['src_id']);
			                $res = Db::field('m.id id,u.username username,u.id uid,photo,content,time')
			                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
			                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
			                    ->find();
			//                halt($res);
			                $list[$k]['old'] = $res;
			                $path = Db::field('path')
			                    ->table(['wb_mpicture'=>'mp'])
			                    ->where('mp.mid = '.$v['src_id'])
			                    ->find();
			//                halt($path);
			                if (!empty($path)){
			                    $list[$k]['old']['path'] = $path['path'];
			                }
			            }
			        }


			        $name = Db::field('style')
			            ->table(['wb_style'])
			            ->where('id=' . $id)
			            ->find();

			//        halt($a);
			        return view('/index/about', [

			            'list' => $list,
			            'res' => $this->res,
			            'banner' => $this->banner,
			            'style' => $this->style,
			            'name' => $name,
			            'sort' => $this->sort,
			            'data1'=>$this->data1,
			            'id' => $uid,
			            'username'=>$username,
			            'photo'=>$photo,
			        ]);

        }
        
        
//        halt($list);

        foreach ($list as $k => $v) {
            $path = Db::field('id pid,path')
                ->table(['wb_mpicture'])
                ->where('mid=' . $v['id'])
                ->order(['id' => 'asc'])
                ->select();
            $list[$k]['path'] = $path;
            if (!empty($v['src_id'])){
//                halt($v['src_id']);
                $res = Db::field('m.id id,u.username username,u.id uid,photo,content,time')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
                    ->find();
//                halt($res);
                $list[$k]['old'] = $res;
                $path = Db::field('path')
                    ->table(['wb_mpicture'=>'mp'])
                    ->where('mp.mid = '.$v['src_id'])
                    ->find();
//                halt($path);
                if (!empty($path)){
                    $list[$k]['old']['path'] = $path['path'];
                }
            }
        }


        $name = Db::field('style')
            ->table(['wb_style'])
            ->where('id=' . $id)
            ->find();

//        halt($a);
        return view('/index/about', [

            'list' => $list,
            'res' => $this->res,
            'banner' => $this->banner,
            'style' => $this->style,
            'name' => $name,
            'sort' => $this->sort,
            'data1'=>$this->data1,
            'id' => $uid,
        ]);


    }


    public function talk()
    {
        $uid = Session::get('id');
        // halt($uid);
        if (empty($uid)) {
        	$talk = Db::field('id,name')
            ->table('wb_talk')
            ->select();
            // halt($talk);
        	return view('/index/features', [
	            'list' => $this->list,
	            'res' => $this->res,
	            'banner' => $this->banner,
	            'style' => $this->style,
	            'sort' => $this->sort,
	            'talk' => $talk,

	            'data1'=>$this->data1,
	            'id' => $uid,
 
        	]);

        }
        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = ' . $uid)
            ->find();
            // halt($p);
        if (!empty($p)) {
            $photo = implode("", $p);
            
        }
        $talk = Db::field('id,name')
            ->table('wb_talk')
            ->select();


        $username = Session::get('username');

        return view('/index/features', [
            'list' => $this->list,
            'res' => $this->res,
            'banner' => $this->banner,
            'style' => $this->style,
            'sort' => $this->sort,
            'talk' => $talk,

            'data1'=>$this->data1,
            'id' => $uid,
            'username'=>$username,
            'photo'=>$photo,


        ]);
    }


    public function login()
    {
        return view('/index/index');
    }


    public function buser()
    {
        return view('/index/buser');
    }



    public function dologin()
    {
        $id = Session::get('id');
        $list = Db::field('src_id,m.id id,u.username username,u.id uid,photo,content,time,collectnum,commentnum,transpondnum,agreenum,readnum,hot,src_id')

            ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i','wb_attent'=>'a'])
            ->where('m.uid = u.id and  i.uid = u.id and a.user_id = '.$id.' and a.attent_id = m.uid')
            ->order(['id' => 'asc'])
            ->select();
//        halt($list);




        foreach ($list as $k => $v) {
            $path = Db::field('id pid,path')
                ->table(['wb_mpicture'])
                ->where('mid=' . $v['id'])
                ->order(['id' => 'asc'])
                ->select();
            $list[$k]['path'] = $path;

            if (!empty($v['src_id'])){
//                halt($v['src_id']);

                $res = Db::field('m.id id,u.username username,u.id uid,photo,content,path,time')
                    ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i','wb_mpicture'=>'mp'])
                    ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])

                    ->find();
//                halt($res);
                $list[$k]['old'] = $res;
                $path = Db::field('path')

                    ->table(['wb_mpicture'=>'mp'])
                    ->where('mp.mid = '.$v['src_id'])
                    ->find();
//                halt($path);
                if (!empty($path)){
                    $list[$k]['old']['path'] = $path['path'];
                }
            }
        }
//        halt($list);


        $res = Db::field('id,name')
            ->table(['wb_talk'])
            ->where('hot=2')
            ->order(['id' => 'asc'])
            ->select();


        $banner = Db::field('id,path,title,link')
            ->table(['wb_banner'])
            ->order(['id' => 'asc'])
            ->select();

//        halt($id);

        $arr = Db::field('role')
            ->table(['wb_buser'])
            ->where('id = ' . $id)
            ->find();

        $role = implode("", $arr);

        $p = Db::field('photo')
            ->table(['wb_buserinfo'])
            ->where('uid = ' . $id)
            ->find();
        $photo = implode("", $p);
//        halt($this->data1);
        $username = Session::get('username');
//        halt(Session::get());
        $uid = Session::get('id');
//        halt(url("index/index/add"));
        return view('/login/dologin', [
            'list' => $list,
            'res' => $res,
            'banner' => $banner,
            'username' => $username,
            'role' => $role,
            'uid' => $uid,
            'style' => $this->style,
            'sort' => $this->sort,
            'data1'=>$this->data1,
            'photo'=>$photo
        ]);


    }

    //跳转注册页面
    public function add()
    {
        return view('/index/add');
    }
    //跳转商业用户注册页
    public function badd()
    {
        return view('/index/badd');
    }


}
